package com.star.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;

@Slf4j
public class HttpUtils {

    public static JSONObject httpGet(String url)  {
        JSONObject jsonObject = new JSONObject();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig config = RequestConfig.custom().setConnectTimeout(3 * 1000) //连接超时时间
                .setConnectionRequestTimeout(3*1000)
                .setSocketTimeout(20 *1000) //数据传输的超时时间
                .build();
        //设置请求配置时间
        URL u;
        URI uri = null;
        try {
            u = new URL(url);
            uri = new URI(u.getProtocol(), u.getHost(), u.getPath(), u.getQuery(), null);
        } catch (URISyntaxException | MalformedURLException e) {
            e.printStackTrace();
        }
        HttpGet httpGet = new HttpGet(uri);
        httpGet.setHeader("Accept-Encoding","gzip, deflate, br");
        httpGet.setConfig(config);
        CloseableHttpResponse response = null;
        InputStream in =null;
        ByteArrayOutputStream result =null;
        try {
            response = httpClient.execute(httpGet);
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            if(Objects.nonNull(entity)){
                in = entity.getContent();
                result = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
                }
                result.flush();
                jsonObject = JSON.parseObject(result.toString("UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(in != null) {
                    in.close();
                }
                if(result != null) {
                    result.close();
                }
                if(response != null) {
                    response.close();
                }
                if(httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }
    public static JSONArray httGet(String url){
        JSONArray jsonArray = new JSONArray();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig config = RequestConfig.custom().setConnectTimeout(3 * 1000) //连接超时时间
                .setConnectionRequestTimeout(3*1000)
                .setSocketTimeout(20 *1000) //数据传输的超时时间
                .build();
        //设置请求配置时间
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Accept-Encoding","gzip, deflate, br");
        httpGet.setConfig(config);
        CloseableHttpResponse response = null;
        InputStream in =null;
        ByteArrayOutputStream result =null;
        try {
            response = httpClient.execute(httpGet);
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            if(Objects.nonNull(entity)){
                in = entity.getContent();
                result = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
                }
                result.flush();
                jsonArray = JSON.parseArray(result.toString("UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(in != null) {
                    in.close();
                }
                if(result != null) {
                    result.close();
                }
                if(response != null) {
                    response.close();
                }
                if(httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonArray;
    }

    public static void close(Closeable... io) {
        for (Closeable temp : io) {
            try {
                if (null != temp) {
                    temp.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
